//
//  XLEmojiManager.m
//
//  Created by ilongge on 2020/7/13.
//  Copyright © 2020 admin. All rights reserved.
//

#import "XLEmojiManager.h"
#import "XLCIViewResourcesManager.h"

@implementation XLEmojiManager

+ (NSAttributedString *)replaceEmojiTextToEmojiImageWithText:(NSString *)text
{
    if (text.length){
        NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text];
        return [XLEmojiManager replaceEmojiTextToEmojiImageWithAttributedText:attributedString];
    }
    return nil;
}

+ (NSAttributedString *)replaceEmojiTextToEmojiImageWithAttributedText:(NSAttributedString *)attributedString
{
    if (attributedString.string.length)
    {
        NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
        NSRange textRange = NSMakeRange(0, mutableAttributedString.string.length);
        NSString *pattern = @"\\[[A-Za-z0-9\u4e00-\u9fa5]{1,4}\\]";
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
        NSArray<NSTextCheckingResult *> *results = [regex matchesInString:mutableAttributedString.string options:NSMatchingReportProgress range:textRange];
        for (NSInteger index = results.count-1; index >= 0 ; index--) {
            NSTextCheckingResult *result = results[index];
            NSString *emojiString = [mutableAttributedString.string substringWithRange:result.range];
            UIImage *emoji = [XLEmojiManager JFLiveEmojiImageWithEmojiString:emojiString];
            if (emoji) {
                NSTextAttachment *attach = [[NSTextAttachment alloc] init];
                attach.image = emoji;
                attach.bounds = CGRectMake(0, 0, 16, 16);
                NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
                [mutableAttributedString replaceCharactersInRange:result.range withAttributedString:attachString];
                [mutableAttributedString addAttribute:NSBaselineOffsetAttributeName
                                                value:@(-3)
                                                range:NSMakeRange(result.range.location, 1)];
            }
        }
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 8;
        paragraphStyle.alignment = NSTextAlignmentLeft;
        NSRange range = NSMakeRange(0, mutableAttributedString.string.length);
        [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
        [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang-SC-Regular" size:14] range:range];
        return [[NSAttributedString alloc] initWithAttributedString:mutableAttributedString];
    }
    return nil;
}

+ (BOOL)isEmojiImageNameForEmojiString:(NSString *)emojiString
{
    if (emojiString.length)
    {
        NSString *emojiName = [XLEmojiManager emojiImageNameForEmojiString:emojiString];
        if (emojiName) {
            return YES;
        }
        else{
            return NO;
        }
    }
    return NO;
}

+ (NSString *)emojiImageNameForEmojiString:(NSString *)emojiString
{
    if (emojiString.length)
    {
        NSDictionary *emojiDictionary = [XLEmojiManager emojiImageNameDictionary];
        NSString *emojiName = emojiDictionary[emojiString];
        return emojiName;
    }
    return nil;
}

+ (NSDictionary *)emojiImageNameDictionary
{
    NSString *jsonPath = [[XLCIViewResourcesManager XLCIResourcesBundle] pathForResource:@"emoji_text_name" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
    NSError *error = nil;
    NSDictionary *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&error]; 
    
    if (error == nil) {
        return jsonRoot;
    }
    else{
        return nil;
    }
}

+ (NSArray *)emojiImageNameArray
{
    NSString *jsonPath = [[XLCIViewResourcesManager XLCIResourcesBundle] pathForResource:@"emoji_text_name_order" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
    NSError *error = nil;
    NSArray *jsonRoot = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&error];
    
    if (error == nil) {
        return jsonRoot;
    }
    else{
        return nil;
    }
}

+ (UIImage *)JFLiveEmojiImageWithEmojiString:(NSString *)emojiString
{
    if (emojiString){
        NSString *emojiName = [XLEmojiManager emojiImageNameForEmojiString:emojiString];
        return [self JFLiveEmojiImageWithName:emojiName];
    }
    return nil;
}

+ (UIImage *)JFLiveEmojiImageWithName:(NSString *)name
{
    return [XLCIViewResourcesManager imageNamed:name];
}

@end
